package com.sam;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Driver;

public class Member {
	private static Connection conn;
	private String userid;
	private String username;
	private String nickname;
	private String password;
	private String email;
	private String status;
	
	private String useridmsg;
	private String usernamemsg;
	private String passwordmsg;
	private String emmailMsg;
	private String nicknamemsg;
	private String loginmsg;
	
	private boolean isLogin;
	private boolean isValid; 
	
	private String referer;
						
	public Member(){
		
	}
	
	protected Member(String userid, String password) {
		super();
		this.userid = userid;
		this.password = password;
	}

	protected Member(String userid, String username, String nickname,
			String password, String email) {
		super();
		this.userid = userid;
		this.username = username;
		this.nickname = nickname;
		this.password = password;
		this.email = email;
	}
	
	protected Member(String userid, String username, String nickname,
			String password, String email, String status) {
		super();
		this.userid = userid;
		this.username = username;
		this.nickname = nickname;
		this.password = password;
		this.email = email;
		this.status = status;
	}

	public boolean checkField() {
		isValid = true;
		
		if(userid.length() < 4 || userid.length() > 20){
			isValid = false;
			useridmsg = "使用者帳號長度不符合!!";
		}
		
		if(username.length() < 4 || username.length() > 20) {
			isValid = false;
			usernamemsg = "使用者名稱長度不符合!!";
		}
		
		if(nickname.length() < 4 || nickname.length() > 20) {
			isValid = false;
			nicknamemsg = "暱稱長度不符合!!";
		}
		
		if(password.length() < 4 || password.length() > 20) {
			isValid = false;
			passwordmsg = "密碼長度不符合!!";
		}
		
		if(email.indexOf("@") == -1) {
			isValid = false;
			emmailMsg = "電子郵件格式不符合!!";
		}
		
		return isValid;
	}
	
	public boolean login(){
		String sql = "select * from users where userid=? and password=?";
		boolean islogin = false;
		    		    		    		
		try {
			PreparedStatement pstmt = getConnection().prepareStatement(sql);
            pstmt.setString(1, userid);
            pstmt.setString(2, password);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()){
            	islogin = true;
            	loginmsg = "登入成功";
            } else {
            	loginmsg = "登入失敗";
            }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return islogin;
	}
	
	public static Connection getConnection() {
        if (conn == null) {
                try {
                        DriverManager.registerDriver(new Driver());
                        conn = DriverManager.getConnection(
                                        "jdbc:mysql://j.snpy.org/j102u17", "j102u17", "j102u17.989");
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
        
        return conn;

}
	
	
	public String getuserid() {
		return userid;
	}

	public void setuserid(String userid) {
		this.userid = userid;
	}

	public String getusername() {
		return username;
	}

	public void setusername(String username) {
		this.username = username;
	}

	public String getusernamemsg() {
		return usernamemsg;
	}

	public void setusernamemsg(String usernamemsg) {
		this.usernamemsg = usernamemsg;
	}

	public String getnickname() {
		return nickname;
	}

	public void setnickname(String nickname) {
		this.nickname = nickname;
	}

	public String getpassword() {
		return password;
	}

	public void setpassword(String password) {
		this.password = password;
	}

	public String getemail() {
		return email;
	}

	public void setemail(String email) {
		this.email = email;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getuseridmsg() {
		return useridmsg;
	}

	public void setuseridmsg(String useridmsg) {
		this.useridmsg = useridmsg;
	}

	public String getpasswordmsg() {
		return passwordmsg;
	}

	public void setpasswordmsg(String passwordmsg) {
		this.passwordmsg = passwordmsg;
	}

	public String getemmailMsg() {
		return emmailMsg;
	}

	public void setemmailMsg(String emmailMsg) {
		this.emmailMsg = emmailMsg;
	}

	public String getnicknamemsg() {
		return nicknamemsg;
	}

	public void setnicknamemsg(String nicknamemsg) {
		this.nicknamemsg = nicknamemsg;
	}

	public String getloginmsg() {
		return loginmsg;
	}

	public void setloginmsg(String loginmsg) {
		this.loginmsg = loginmsg;
	}

	public boolean isLogin() {
		return isLogin;
	}

	public void setLogin(boolean isLogin) {
		this.isLogin = isLogin;
	}

	public boolean isValid() {
		return isValid;
	}

	public void setValid(boolean isValid) {
		this.isValid = isValid;
	}

	public String getReferer() {
		if (referer!=null && referer.substring(referer.lastIndexOf("/")).equals("/index.jsp")){
			referer = null;
		}
		return referer;
	}

	public void setReferer(String referer) {
		this.referer = referer;
		System.out.println("setReferer:"+referer);
	}
}
